package com.ttbj.core.dao.redis.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.ttbj.core.dao.redis.IRedisBaseDao;
import com.ttbj.core.dao.redis.IRedisCommonBaseDao;
import com.ttbj.core.util.JsonUtil;
import redis.clients.jedis.ShardedJedis;

 

/**
 * @Description: redisCommonBaseDao 实现类
 * 
 * @Author: Daniel
 * @Version: V1.0
 * 
 */
@Repository("redisCommonBaseDao")
public class RedisCommonBaseDaoImpl implements IRedisCommonBaseDao {

	private static final Logger logger = LoggerFactory.getLogger(RedisCommonBaseDaoImpl.class);
	@Autowired
	private IRedisBaseDao redisBaseDao;

	@Override
	public Long decrBy(String key, long integer) {
		ShardedJedis shardedJedis = redisBaseDao.getResource();
		boolean broken = false;
		Long rslt = null;
		try {
			rslt = shardedJedis.decrBy(key, integer);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			broken = true;
		} finally {
			redisBaseDao.returnResource(shardedJedis, broken);
		}
		return rslt;
	}

	@Override
	public Long decr(String key) {
		ShardedJedis shardedJedis = redisBaseDao.getResource();
		boolean broken = false;
		Long rslt = null;
		try {
			rslt = shardedJedis.decr(key);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			broken = true;
		} finally {
			redisBaseDao.returnResource(shardedJedis, broken);
		}
		return rslt;
	}

	@Override
	public Long incrBy(String key, long integer) {
		ShardedJedis shardedJedis = redisBaseDao.getResource();
		boolean broken = false;
		Long rslt = null;
		try {
			rslt = shardedJedis.incrBy(key, integer);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			broken = true;
		} finally {
			redisBaseDao.returnResource(shardedJedis, broken);
		}
		return rslt;
	}

	@Override
	public Double incrByFloat(String key, double integer) {
		ShardedJedis shardedJedis = redisBaseDao.getResource();
		boolean broken = false;
		Double rslt = null;
		try {
			rslt = shardedJedis.incrByFloat(key, integer);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			broken = true;
		} finally {
			redisBaseDao.returnResource(shardedJedis, broken);
		}
		return rslt;
	}

	@Override
	public Long incr(String key) {
		String logHeader = Thread.currentThread().getStackTrace()[1].getMethodName();
		logger.debug("{} -S key:{}", logHeader, key);
		ShardedJedis shardedJedis = redisBaseDao.getResource();
		boolean broken = false;
		Long result = null;
		try {
			result = shardedJedis.incr(key);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			broken = true;
		} finally {
			redisBaseDao.returnResource(shardedJedis, broken);
		}
		logger.debug("{} -E {}", logHeader, JsonUtil.beanToJson(result));
		return result;
	}
}
